package com.wf.course.web.controller.ad;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.ColorUIResource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.utils.page.PageBean;
import com.wf.course.model.ad.CourseCategory;
import com.wf.course.service.ICourseCategoryService;
import com.wf.course.web.Interceptor.LogInfo;

/**
 * 课程分类
 * @author TT
 *
 */
@RequestMapping("CourseCategory")
@Controller
public class CourseCategoryController {
	@Autowired
	private ICourseCategoryService courseCategoryService;
	
	/**
	 * 获取课程分类：列表
	 * @param pageNo
	 * @param pageSize
	 * @param name
	 * @param model
	 * @return
	 */
	@LogInfo("获取课程分类：列表")
	@RequestMapping("CourseCategoryList")
	public String CourseCategoryList(
			@RequestParam(value="pageNo",required=false) Integer pageNo,
			@RequestParam(value="pageSize",required=false) Integer pageSize,
			@RequestParam(value="name",required=false) String name,
			@RequestParam(value="parentId",required=false) Integer parentId,
			Model model
	){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("parentId", parentId);
		
		PageBean<CourseCategory> pageBean = new PageBean<CourseCategory>(pageNo,pageSize,null,map);
		
		courseCategoryService.getCourseCategoryList(pageBean);
		pageBean.setUrl("/CourseCategory/CourseCategoryList");
		Integer pid = 0;
		List<CourseCategory> list = courseCategoryService.getParentCourseCategory(pid);
		
		model.addAttribute("page",pageBean);
		model.addAttribute("list",list);
		return "ad/courseCategoryList";
	}
	
	/**
	 * 新增/修改课程分类，页面数据初始化
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增/修改课程分类，页面数据初始化")
	@RequestMapping("CourseCategoryLoad/{id}")
	public String CourseCategoryLoad(@PathVariable(value="id") Integer id,Model model){
		if (id != null && id != 0) {
			//修改	
			CourseCategory courseCategory = this.courseCategoryService.getCourseCategory(id);
			model.addAttribute("courseCategory",courseCategory);
			model.addAttribute("type","upd");
		}else {
			//新增
			model.addAttribute("type","add");
		}
		//获取下拉列表内容
		List<CourseCategory> list = this.courseCategoryService.getCourseCategoryAllList();
		model.addAttribute("ddl",list);
		
		return "ad/courseCategoryEdit";
	}
	
	/**
	 * 新增或修改课程分类
	 * @param courseCategory
	 * @param imgFile
	 * @param request
	 * @return
	 */
	@LogInfo("新增或修改课程分类")
	@RequestMapping(value = "CourseCategoryEdit",produces = "text/html;charset=UTF-8")
	public String CourseCategoryEdit(@ModelAttribute CourseCategory courseCategory, MultipartFile imgFile,HttpServletRequest request){
		courseCategoryService.saveCourseCategory(courseCategory,imgFile);
		return "redirect:/CourseCategory/CourseCategoryList";
	}
	
	/**
	 * 根据ID删除一条课程分类
	 * @param id
	 * @return
	 */
	@LogInfo("删除课程分类")
	@RequestMapping("CoureseCategoryDel/{id}")
	public String CoureseCategoryDel(@PathVariable Integer id){
		this.courseCategoryService.deleteCourseCategory(id);
		return "redirect:/CourseCategory/CourseCategoryList"; 
	}
}
